﻿//======================================================================
//
//        Copyright (C) 2015   
//        All rights reserved
//
//        filename : PutInAllocation
//        description : 半成品/成品卡调拨入库
//        created by XXX at  20150821
//
//======================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CardLogistics.Web.Components;
using CardLogistics.Model.CardMaking;
using System.Data;

namespace CardLogistics.Web.MainPage.PutOutCard.PutInAllocation
{
    public partial class AddEdit : BasePage
    {
        private static readonly CardLogistics.DAL.PutOutCard.PutInAllocationData DAL = new CardLogistics.DAL.PutOutCard.PutInAllocationData();
        private static readonly CardLogistics.DAL.PutOutCard.PutOutSendCard_DetailData SendDAL = new CardLogistics.DAL.PutOutCard.PutOutSendCard_DetailData();

        public string TitleName
        {
            get;
            set;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TitleName = GetStatusInfo(-1, "半成品/成品卡调拨入库");
                InitControl();
            }
        }


        #region 编辑时绑定值
        /// <summary>
        /// 编辑时绑定值
        /// </summary>
        public void InitControl()
        {
            if (IsEdit)
            {
                string _key = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(_key))
                {
                    CardLogistics.Model.PutOutCard.PutInAllocation pData = DAL.GetModel(_key);
                    SetWebControls(this.Page, pData);
                    hf_SendId.Value = pData.SENDCARDID;
                    hfEditSumCard.Value = Convert.ToInt32(pData.ALLOCATIONNUMBER).ToString(); //编辑前的调入数量
                }
            }
          else
            {
                ALLOCATIONNO.Value = GetBillNumber("半成品/成品卡调拨入库");
            }

        }
        #endregion
       
        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            try
            {
                string result = EncapsulationModel(0);
                if (result.Split(',')[0] == "ok")
                {
                    InsertLog("保存半成品/成品卡调拨入库信息,调拨入库单号:" + ALLOCATIONNO.Value);
                    ReturnListInfo();
                }
                else if (result.Split(',')[1] == "-1")
                {
                    ShowMessageBox("调拨入库单号已存在,不允许重复！");
                    return;
                }
                else if (result.Split(',')[1] == "-3")
	            {
                    ShowMessageBox("【调拨入库数量】必须小于等于【发放单中的卡数量】");
                    ALLOCATIONNUMBER.Focus();
                    return;
	            }
                else
                {
                    ShowMessageBox("操作失败请重新操作！" + result.Split(',')[1]);
                }
            }
            catch (Exception ex)
            {
                ShowMessageBox("系统异常:" + ex.ToString());
            }
        }

        /// <summary>
        /// 半成品/成品卡调拨入库信息
        /// </summary>
        /// <param name="issubmit">是否提交</param>
        /// <returns></returns>
        private string EncapsulationModel(int issubmit)
        {
            string result = string.Empty;

            CardLogistics.Model.PutOutCard.PutInAllocation model = new Model.PutOutCard.PutInAllocation();
            model = GetWebControls<CardLogistics.Model.PutOutCard.PutInAllocation>(this.Page);
            model.LASTUPDATEUSER = LoggedUser.ID;
            model.LASTUPDATEDATE = DateTime.Now;
            model.ADDTIME = DateTime.Now;
            model.ISDELETE = 0;
            model.SENDCARDID = hf_SendId.Value; //发放单号Id

           int sendCardNumber = 0;//卡片发放数量
           int allocationNumber = 0;//当前数量
           int putInedNumber = 0; //已调拨入库数量
           int putInOkNumber = 0; //可调拨入库数量

            DataTable dt = SendDAL.GetInfoBySendcardId(hf_SendId.Value);
            if (dt.Rows.Count>0)
            {
                sendCardNumber = Convert.ToInt32(dt.Compute("sum(ACTUALNUMBER)", ""));//卡片发放数量
            }
            DataTable putIneddt = DAL.GetInfoBySendCardId(hf_SendId.Value);
            if (putIneddt.Rows.Count > 0)
            {
                putInedNumber = Convert.ToInt32(putIneddt.Compute("sum(allocationnumber)", ""));//已调拨入库数量
            }
                allocationNumber = Convert.ToInt32(ALLOCATIONNUMBER.Value);//调拨入库数量

            if (IsEdit)
            {
                //编辑时：可调拨入库数量=卡片发放数量-已调拨入库数量+当前数量
                putInOkNumber = sendCardNumber - putInedNumber + Convert.ToInt32(hfEditSumCard.Value);
                if (allocationNumber > putInOkNumber)
                {
                    return result = "err,-3";
                }
                else
                {
                    model.ID = Request.QueryString["id"];
                    result = DAL.Update(model);
                }
            }
            else
            {
                //新增时：可调拨入库数量=卡片发放数量-已调拨入库数量
                putInOkNumber = sendCardNumber - putInedNumber;
                if (allocationNumber > putInOkNumber)
                {
                    return result = "err,-3";
                }
                else
                {
                    ChgBillNumber("半成品/成品卡调拨入库");
                    result = DAL.Add(model);
                }
            }
            return result;
        }
    }
}